package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.RoadmapCm;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface RoadmapCmDao {

    @Results(id = "roadmapCmResultMap", value = {
            @Result(column = "roadmap_cm_param_id", property = "roadmapCmParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "roadmap_cm_param_key", property = "roadmapCmParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_cm_param_value1", property = "roadmapCmParamValue1", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_cm_param_value2", property = "roadmapCmParamValue2", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_cm_param_value3", property = "roadmapCmParamValue3", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_company_id", property = "pcbaCompanyId", jdbcType = JdbcType.INTEGER),
            @Result(column = "ability_mismatch_causes1", property = "abilityMismatchCauses1", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ability_improve_plan1", property = "abilityImprovePlan1", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ability_mismatch_causes2", property = "abilityMismatchCauses2", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ability_improve_plan2", property = "abilityImprovePlan2", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ability_mismatch_causes3", property = "abilityMismatchCauses3", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ability_improve_plan3", property = "abilityImprovePlan3", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version_y", property = "versionY", jdbcType = JdbcType.INTEGER),
            @Result(column = "version_n", property = "versionN", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select * from " + DB1TableName.ROADMAP_CM + " where pcba_company_id = #{pcbaCompanyId}")
    List<RoadmapCm> getAllRoadmapCMByCompany(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("versionY") Integer versionY, @Param("versionN") String versionN);

    /**
     * 根据工厂id和key查询
     *
     * @param pcbaCompanyId
     * @param roadmapCmParamKey
     * @return
     */
    @ResultMap(value = "roadmapCmResultMap")
    @Select("select * from " + DB1TableName.ROADMAP_CM + " where pcba_company_id = #{pcbaCompanyId} and roadmap_cm_param_key = #{roadmapCmParamKey} and version_y = #{versionY} and version_n = #{versionN} order by create_time desc limit 1")
    RoadmapCm getRoadmapCmByCompanyAndKey(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("roadmapCmParamKey") String roadmapCmParamKey, @Param("versionY") Integer versionY, @Param("versionN") String versionN);

    /**
     * 根据key and company_id删除
     */
    @Delete("delete from " + DB1TableName.ROADMAP_CM + " where roadmap_cm_param_key = #{roadmapCmParamKey} and pcba_company_id = #{pcbaCompanyId} and version_y = #{versionY} and version_n = #{versionN}")
    Integer deleteRoadmapCmByKeyAndCompany(@Param("roadmapCmParamKey") String roadmapCmParamKey, @Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("versionY") Integer versionY, @Param("versionN") String versionN);

    /**
     * 新增
     */
    @Insert("insert into " + DB1TableName.ROADMAP_CM + "(roadmap_cm_param_key,create_time,sort,status,roadmap_cm_param_value1,roadmap_cm_param_value2,roadmap_cm_param_value3,ability_mismatch_causes1,ability_improve_plan1,ability_mismatch_causes2,ability_improve_plan2,ability_mismatch_causes3,ability_improve_plan3,pcba_company_id,version_y,version_n) values " +
            "(#{roadmapCm.roadmapCmParamKey},#{roadmapCm.createTime},#{roadmapCm.sort},#{roadmapCm.status},#{roadmapCm.roadmapCmParamValue1},#{roadmapCm.roadmapCmParamValue2},#{roadmapCm.roadmapCmParamValue3},#{roadmapCm.abilityMismatchCauses1},#{roadmapCm.abilityImprovePlan1},#{roadmapCm.abilityMismatchCauses2},#{roadmapCm.abilityImprovePlan2},#{roadmapCm.abilityMismatchCauses3},#{roadmapCm.abilityImprovePlan3},#{roadmapCm.pcbaCompanyId},#{roadmapCm.versionY},#{roadmapCm.versionN})")
    Integer addRoadmapCm(@Param("roadmapCm") RoadmapCm roadmapCm);

    /**
     * 更新
     */
    @Update("<script>" +
            "update " + DB1TableName.ROADMAP_CM +
            "<set>" +
            "<if test='roadmapCm.roadmapCmParamKey != null and roadmapCm.roadmapCmParamKey != \"\" '>" +
            " roadmap_cm_param_key = #{roadmapCm.roadmapCmParamKey}," +
            "</if>" +
            "<if test='roadmapCm.roadmapCmParamValue1 != null '>" +
            " roadmap_cm_param_value1 = #{roadmapCm.roadmapCmParamValue1}," +
            "</if>" +
            "<if test='roadmapCm.roadmapCmParamValue2 != null '>" +
            " roadmap_cm_param_value2 = #{roadmapCm.roadmapCmParamValue2}," +
            "</if>" +
            "<if test='roadmapCm.roadmapCmParamValue3 != null '>" +
            " roadmap_cm_param_value3 = #{roadmapCm.roadmapCmParamValue3}," +
            "</if>" +
            "<if test='roadmapCm.abilityMismatchCauses1 != null '>" +
            " ability_mismatch_causes1 = #{roadmapCm.abilityMismatchCauses1}," +
            "</if>" +
            "<if test='roadmapCm.abilityMismatchCauses2 != null '>" +
            " ability_mismatch_causes2 = #{roadmapCm.abilityMismatchCauses2}," +
            "</if>" +
            "<if test='roadmapCm.abilityMismatchCauses3 != null '>" +
            " ability_mismatch_causes3 = #{roadmapCm.abilityMismatchCauses3}," +
            "</if>" +
            "<if test='roadmapCm.abilityImprovePlan1 != null '>" +
            " ability_improve_plan1 = #{roadmapCm.abilityImprovePlan1}," +
            "</if>" +
            "<if test='roadmapCm.abilityImprovePlan2 != null '>" +
            " ability_improve_plan2 = #{roadmapCm.abilityImprovePlan2}," +
            "</if>" +
            "<if test='roadmapCm.abilityImprovePlan3 != null '>" +
            " ability_improve_plan3 = #{roadmapCm.abilityImprovePlan3}," +
            "</if>" +
            "<if test='roadmapCm.pcbaCompanyId != null and roadmapCm.pcbaCompanyId != \"\" '>" +
            " pcba_company_id = #{roadmapCm.pcbaCompanyId}," +
            "</if>" +
            "<if test='roadmapCm.updateTime != null '>" +
            " update_time = #{roadmapCm.updateTime}," +
            "</if>" +
            "<if test='roadmapCm.sort != null and roadmapCm.sort != \"\" '>" +
            " sort = #{roadmapCm.sort}," +
            "</if>" +
            "<if test='roadmapCm.status != null and roadmapCm.status != \"\" '>" +
            " status = #{roadmapCm.status}," +
            "</if>" +
            "<if test='roadmapCm.versionY != null and roadmapCm.versionY != \"\" '>" +
            " version_y = #{roadmapCm.versionY}," +
            "</if>" +
            "<if test='roadmapCm.versionN != null and roadmapCm.versionN != \"\" '>" +
            " version_n = #{roadmapCm.versionN}," +
            "</if>" +
            "</set>" +
            " where roadmap_cm_param_id = #{roadmapCm.roadmapCmParamId}" +
            "</script>")
    Integer updateRoadmapCm(@Param("roadmapCm") RoadmapCm roadmapCm);
}
